package com.axs.logcollector;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.log4j.Logger;

import com.axs.logcollector.axs.AxSManagementService;
import com.axs.logcollector.jira.JiraManagementService;
import com.axs.logcollector.utils.LogCollectorUtils;

public class LogCollectorService {
	private static final Logger m_log = Logger.getLogger(LogCollectorService.class); 
	
	private JiraManagementService m_jiraManagementService;
	private AxSManagementService m_axsManagementService;
	
	public LogCollectorService() {
		m_jiraManagementService = new JiraManagementService();
		m_axsManagementService = new AxSManagementService();
	}
	
	public void attachLogs(LogCollector logCollector) {
		// Logs is selected to attach on server 
		Map<String, List<String>> selectedLogs = logCollector.getSelectedLogs();
		for (Entry<String, List<String>> logEntry : selectedLogs.entrySet()) {
			// Copy logs from server Ip 
			m_log.info("Collecting logs from " + logEntry.getKey());
			m_axsManagementService.copyLogsToLocal(logEntry.getKey(), logEntry.getValue());
		}
		
		// Compress collected logs to zip file
		String localDir = LogCollectorUtils.getTmpLogFileDir();
		String zipFilename = LogCollectorUtils.getTmpZipFileDir() + File.separator + logCollector.getLogName();
		LogCollectorUtils.zipLogFiles(localDir, zipFilename);
		
		// Checking collected logs should be uploaded to JIRA ticket or FTP server 
		if (logCollector.getAttachmentType().equals(AttachmentType.JIRA_TICKET)) {
			// Attach log to JIRA ticket
			attachLogsToJIRA(logCollector, zipFilename);
		} if (logCollector.getAttachmentType().equals(AttachmentType.FTP_SERVER)) {
			// Attach log to FTP server 
		}
		
		// Add comment on JIRA ticket 
		if (logCollector.getComment() != null) {
			addCommentToJIRA(logCollector);
		}
		
		// Delete tmp file if logs is attached successfully 
		LogCollectorUtils.deleteTmpDirectory();
	}
	
	private void attachLogsToJIRA(LogCollector logCollector, String compressedLogFile) {
		m_jiraManagementService.addAttachment(logCollector, compressedLogFile);
	}
	
	private void addCommentToJIRA(LogCollector logCollector) {
		m_jiraManagementService.addComment(logCollector);
	}
	
	public void attachLogsToFTP(LogCollector logCollector) {
		
	}
	
	public Set<String> getAllServerIPs(String serverIp) {
		return getAllServerIpsAndLogs(serverIp).keySet();
	}
	
	public Map<String, List<String>> getAllServerIpsAndLogs(String serverIp) {
		return m_axsManagementService.getAllServerIpsAndLogs(serverIp);
	}
	
	public String getAMSLogDir(String serverIp) {
		return m_axsManagementService.getAMSLogDirectory(serverIp);
	}
	
	public void disconnect() {
		
	}
	
	public static void main(String[] args) {
		LogCollectorService service = new LogCollectorService();
		String serverIp = "192.168.95.110";
		Map<String, List<String>> serverIpsAndLogs = service.getAllServerIpsAndLogs(serverIp);
		Map<String, List<String>> newServerIpsAndLogs = new HashMap<String, List<String>>();
		for (String server : serverIpsAndLogs.keySet()) {
			List<String> logs = new ArrayList<String>();
			logs.add(service.getAMSLogDir(server) + "/" + "server.log");
			logs.add(service.getAMSLogDir(server) + "/" + "security.log");
			newServerIpsAndLogs.put(server, logs);
		}
		
		LogCollector logCollector = new LogCollector();
		logCollector.setAttachmentType(AttachmentType.JIRA_TICKET);
		logCollector.setComment("Attached logs at attachedlog_test.zip");
		logCollector.setCompress(true);
		logCollector.setJiraAddress("http://tiger.in.alcatel-lucent.com:8091");
		logCollector.setJiraUserName("knguyenn");
		logCollector.setJiraPassword("s1515_k1515");
		logCollector.setLogName("attachedlog_test.zip");
		logCollector.setSelectedLogs(newServerIpsAndLogs);
		logCollector.setServerIp(serverIp);
		logCollector.setTicketId("CONV-75362");
		
		m_log.info(">>>>>>>>>>>>>>>>>>>>> Staring attach logs on ticket" + (new Date()));
		service.attachLogs(logCollector);
		m_log.info(">>>>>>>>>>>>>>>>>>>>> End attach logs on ticket" + (new Date()));
	}
}
